﻿package plugin.union.game
{
    import com.core.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.utils.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.union.*;
    import uiresource.union.*;
    import view.message.*;

    public class UnionListGame extends UnionListUI implements IPage
    {
        private var isOnline:int = 0;
        private var timer:int = 0;
        private var timeDic:Dictionary;

        public function UnionListGame()
        {
            this.timeDic = new Dictionary();
            this.initEvent();
            return;
        }// end function

        private function initEvent() : void
        {
            btn_change.clickHandler = new Handler(this.toChange);
            cb_filter.clickHandler = new Handler(this.changeFilter);
            GlobalLayer.instance.root.addEventListener(MouseEvent.MOUSE_DOWN, this.doStop);
            return;
        }// end function

        protected function doStop(event:MouseEvent) : void
        {
            menu.visible = false;
            return;
        }// end function

        private function changeFilter() : void
        {
            this.isOnline = int(cb_filter.selected);
            this.refresh();
            return;
        }// end function

        private function toChange() : void
        {
            var openday:int;
            var str:String;
            switch(btn_change.label)
            {
                case "创建行会":
                {
                    ModelProxy.change("plugin", "open", {target:"unioncreate"});
                    break;
                }
                case "退出行会":
                {
                    openday = (int(ModelProxy.getValue("data", "openday")) + 1);
                    str;
                    if (openday == 1)
                    {
                        str;
                    }
                    Alert2.show(str, "行会退出提示", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    UnionSender._41quit();
                }
                return;
            }// end function
            );
                    break;
                }
                case "解散行会":
                {
                    openday = (int(ModelProxy.getValue("data", "openday")) + 1);
                    str;
                    if (openday == 1)
                    {
                        str;
                    }
                    Alert2.show(str, "行会解散提示", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    UnionSender._23dissolution();
                }
                return;
            }// end function
            );
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function refresh() : void
        {
            var _loc_5:Object = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:Object = null;
            var _loc_9:Object = null;
            var _loc_1:* = UserData.getUserData("unionid");
            if (_loc_1 > 0)
            {
                btn_change.label = "退出行会";
                txt_state.text = "状态";
            }
            else
            {
                btn_change.label = "创建行会";
                txt_state.text = "操作";
            }
            var _loc_2:* = ModelProxy.getValue("data", "unionsInfo");
            var _loc_3:Array = [];
            var _loc_4:* = UserData.getUserData("name");
            for each (_loc_5 in _loc_2)
            {
                
                if (_loc_5.masterOnline >= this.isOnline)
                {
                    _loc_3.push(_loc_5);
                }
                if (_loc_5.unionMaster == _loc_4)
                {
                    btn_change.label = "解散行会";
                }
            }
            _loc_6 = _loc_3.length - 1;
            while (_loc_6 >= 0)
            {
                
                _loc_7 = 0;
                while (_loc_7 < _loc_6)
                {
                    
                    _loc_8 = _loc_3[_loc_7];
                    _loc_9 = _loc_3[(_loc_7 + 1)];
                    if (int(_loc_8.unionLevel) < int(_loc_9.unionLevel))
                    {
                        this.swap(_loc_3, _loc_7, (_loc_7 + 1));
                    }
                    else if (int(_loc_8.unionLevel) == int(_loc_9.unionLevel))
                    {
                        if (int(_loc_8.money) < int(_loc_9.money))
                        {
                            this.swap(_loc_3, _loc_7, (_loc_7 + 1));
                        }
                        else if (int(_loc_8.money) == int(_loc_9.money))
                        {
                            if (int(_loc_8.memberCount) < int(_loc_9.memberCount))
                            {
                                this.swap(_loc_3, _loc_7, (_loc_7 + 1));
                            }
                            else if (int(_loc_8.memberCount) == int(_loc_9.memberCount))
                            {
                                if (int(_loc_8.creatTime) > int(_loc_9.creatTime))
                                {
                                    this.swap(_loc_3, _loc_7, (_loc_7 + 1));
                                }
                            }
                        }
                    }
                    _loc_7++;
                }
                _loc_6 = _loc_6 - 1;
            }
            list_union.array = _loc_3;
            list_union.renderHandler = new Handler(this.unionRender);
            return;
        }// end function

        private function swap(param1:Array, param2:int, param3:int) : void
        {
            var _loc_4:* = param1[param2];
            param1[param2] = param1[param3];
            param1[param3] = _loc_4;
            return;
        }// end function

        private function unionRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:UnionListItemUI = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            if (param2 < list_union.length)
            {
                _loc_3 = list_union.array[param2];
                _loc_4 = param1 as UnionListItemUI;
                _loc_4.txt_name.text = _loc_3.unionName;
                _loc_4.txt_master.text = _loc_3.unionMaster;
                _loc_4.txt_level.text = _loc_3.unionLevel;
                _loc_4.txt_members.text = _loc_3.memberCount + "/" + ConfigDictionary.data.union[(_loc_3.unionLevel - 1)].count;
                _loc_5 = int((param2 + 1) / 10);
                _loc_6 = int((param2 + 1)) % 10;
                _loc_4.num0.url = "png.common." + _loc_5;
                _loc_4.num1.url = "png.common." + _loc_6;
                if (param2 < 3)
                {
                    _loc_4.rank.url = "cs2_rank/hg" + (param2 + 1) + ".png";
                    _loc_4.rank.x = _loc_4.num1.x;
                    _loc_4.rank.visible = true;
                    _loc_4.num0.visible = false;
                    _loc_4.num1.visible = false;
                }
                else
                {
                    _loc_4.num0.visible = _loc_5 != 0;
                    _loc_4.num1.visible = true;
                    _loc_4.rank.visible = false;
                }
                _loc_7 = UserData.getUserData("unionid");
                delete this.timeDic[_loc_4.state];
                if (_loc_7 == 0)
                {
                    _loc_4.state.label = "申请";
                    _loc_4.state.x = 650;
                    _loc_4.state.mouseEnabled = true;
                    _loc_4.state.underLine = true;
                    _loc_4.state.labelColors = "0x54b436,0xefd742,0x54b436,0xefd742";
                    _loc_4.state.removeEventListener(MouseEvent.CLICK, this.doLink);
                    _loc_4.state.addEventListener(MouseEvent.CLICK, this.doLink);
                    _loc_4.state.dataSource = _loc_3;
                    param1.removeEventListener(MouseEvent.CLICK, this.showMenu);
                }
                else
                {
                    _loc_4.state.removeEventListener(MouseEvent.CLICK, this.doLink);
                    param1.removeEventListener(MouseEvent.CLICK, this.showMenu);
                    param1.addEventListener(MouseEvent.CLICK, this.showMenu);
                    switch(int(_loc_3.relation))
                    {
                        case 0:
                        {
                            _loc_4.state.label = "正常";
                            _loc_4.state.x = 650;
                            _loc_4.state.mouseEnabled = false;
                            _loc_4.state.underLine = false;
                            _loc_4.state.labelColors = "0x54b436,0x54b436,0x54b436,0x54b436";
                            break;
                        }
                        case 1:
                        {
                            _loc_4.state.label = "同盟";
                            _loc_4.state.x = 650;
                            _loc_4.state.labelColors = "0x54b436,0x54b436,0x54b436,0x54b436";
                            _loc_4.state.mouseEnabled = false;
                            _loc_4.state.underLine = false;
                            break;
                        }
                        case 2:
                        {
                            _loc_8 = Number(ModelProxy.getValue("data", "servertime") + getTimer()) / 1000;
                            _loc_9 = _loc_3.unfriendEndTime - _loc_8;
                            if (_loc_9 > 0)
                            {
                                _loc_4.state.label = "宣战(" + TimeDataFormatUtil.getLeftTime2(_loc_9) + ")";
                                _loc_4.state.mouseEnabled = false;
                                _loc_4.state.x = 624;
                                _loc_4.state.underLine = false;
                                _loc_4.state.labelColors = "0xc72514,0xc72514,0xc72514,0xc72514";
                                this.timeDic[_loc_4.state] = _loc_9;
                                if (this.timer == 0)
                                {
                                    this.timer = setInterval(this.doTime, 1000);
                                }
                            }
                            else
                            {
                                _loc_3.relation = 0;
                                _loc_4.state.label = "正常";
                                _loc_4.state.x = 650;
                                _loc_4.state.mouseEnabled = false;
                                _loc_4.state.underLine = false;
                                _loc_4.state.labelColors = "0x54b436,0x54b436,0x54b436,0x54b436";
                            }
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
            }
            return;
        }// end function

        private function doTime() : void
        {
            var _loc_1:Object = null;
            for (_loc_1 in this.timeDic)
            {
                
                var _loc_4:* = this.timeDic;
                var _loc_5:* = _loc_1;
                var _loc_6:* = this.timeDic[_loc_1] - 1;
                _loc_4[_loc_5] = _loc_6;
                _loc_1.label = "宣战(" + TimeDataFormatUtil.getLeftTime2(this.timeDic[_loc_1]) + ")";
            }
            return;
        }// end function

        protected function showMenu(event:MouseEvent) : void
        {
            var _loc_3:Array = null;
            var _loc_6:Object = null;
            var _loc_2:* = event.currentTarget.dataSource;
            if (_loc_2.unionId == UserData.getUserData("unionid"))
            {
                return;
            }
            var _loc_4:* = ModelProxy.getValue("data", "unionInfo");
            var _loc_5:* = UserData.getUserData("rid");
            if (_loc_4)
            {
                for each (_loc_6 in _loc_4.memberList)
                {
                    
                    if (_loc_6.rid == _loc_5)
                    {
                        if (_loc_6.title >= 10)
                        {
                            break;
                            continue;
                        }
                        return;
                    }
                }
            }
            else
            {
                return;
            }
            switch(int(_loc_2.relation))
            {
                case 0:
                {
                    _loc_3 = [{txt:"宣战", id:_loc_2.unionId}];
                    break;
                }
                case 1:
                {
                    return;
                }
                case 2:
                {
                    return;
                }
                default:
                {
                    break;
                }
            }
            menu.visible = true;
            list_menu.array = _loc_3;
            list_menu.renderHandler = new Handler(this.menuHandler);
            menuBg.height = 20 * _loc_3.length;
            menu.x = mouseX;
            menu.y = mouseY;
            return;
        }// end function

        private function menuHandler(param1:Component, param2:int) : void
        {
            var _loc_3:Label = null;
            if (param2 < list_menu.length)
            {
                _loc_3 = param1.getChildByName("txt") as Label;
                _loc_3.text = list_menu.array[param2].txt;
                param1.addEventListener(MouseEvent.MOUSE_DOWN, this.doDown);
                param1.addEventListener(MouseEvent.CLICK, this.doClick);
            }
            return;
        }// end function

        protected function doClick(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget.dataSource;
            switch(_loc_2.txt)
            {
                case "同盟":
                {
                    UnionSender._35alliance(_loc_2.id);
                    break;
                }
                case "宣战":
                {
                    UnionSender._40declarationOfWar(_loc_2.id);
                    break;
                }
                case "解除同盟":
                {
                    UnionSender._39endAlliance(_loc_2.id);
                    break;
                }
                default:
                {
                    break;
                }
            }
            menu.visible = false;
            return;
        }// end function

        protected function doDown(event:MouseEvent) : void
        {
            event.stopPropagation();
            return;
        }// end function

        protected function doLink(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget.dataSource;
            UnionSender._8applyForAdd(_loc_2.unionId);
            return;
        }// end function

        public function update(param1:int, param2:Object) : void
        {
            return;
        }// end function

    }
}
